package com.css.apps.base.common.provider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.model.SUserPost;
import com.css.apps.base.user.model.SUserRole;
import com.css.db.query.QueryCache;
import com.css.ids.server.service.dao.IdsUserDao;
import com.css.util.StringHelper;
import com.css.webbase.WebBaseUtil;

/**
 * 系统用户操作定义,主要定义查询操作
 * 
 * @author paladin
 */
public class UserQueryProvider {
	
	private static Log log = LogFactory.getLog(UserQueryProvider.class);
	
	/**
	 * 获取当前用户
	 */
	public static SUser currentUser() {
		return (SUser) WebBaseUtil.getCurrentUser();
	}

	public static SUser getUser(String userId) {
		return QueryCache.get(SUser.class, userId);
	}
	
	public static String getUserNames(String userIds) {
		List<String> names = new ArrayList<String>();
		List<String> userIdList = StringHelper.strToList(userIds);
		if (userIdList != null) {
			for (String userId : userIdList) {
				SUser user = getUser(userId);
				if (user == null) {
					continue;
				}
				names.add(user.getRealName());
			}
		}
		return StringHelper.listToStr(names);
	}

	@SuppressWarnings("unchecked")
	public static Map<String, List<SUser>> getUsers() {
		Map<String, List<SUser>> result = new HashMap<>();
		List<Object[]> list = new QueryCache("select a.orgId,a.uuid from SUser a  order by a.orderNum").list();
		if (list != null && list.size() > 0) {
			for (Object[] os : list) {
				List<SUser> users = result.get(os[0]);
				if (users == null) {
					users = new ArrayList<SUser>();
				}
				users.add(QueryCache.get(SUser.class, (String) os[1]));
				result.put((String) os[0], users);
			}
		}
		return result;
	}

	/**
	 * 获取总部用户
	 * 
	 * @author liuhy 2019-2019年3月15日
	 */
	public static List<SUser> listHeaderQuearterUsers() {
		return IdsUserDao.listAllUser(1);
	}

	/**
	 * 获取所有用户
	 * 
	 * @author liuhy 2019-2019年3月15日
	 */
	public static List<SUser> listAllUser() {
		return IdsUserDao.listAllUser();
	}
	
	@SuppressWarnings("unchecked")
	public static List<SUserRole> getSUserRoleByRoleId(String roleId) {
		List<SUserRole> userRoleList = null;
		try {
			QueryCache qc = new QueryCache("select a.uuid from SUserRole  a where a.roleId=:roleId ")
					.setParameter("roleId", roleId);
			List<Integer> Ids = qc.listCache();
			if (Ids != null)
				userRoleList = QueryCache.idToObj(SUserRole.class, Ids);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return userRoleList;
	}
	
	@SuppressWarnings("unchecked")
	public static List<SUserPost> getSUserPostByPostId(String postId) {
		List<SUserPost> userPostList = null;
		try {
			QueryCache qc = new QueryCache("select a.uuid from SUserPost  a where a.postId=:postId ")
					.setParameter("postId", postId);
			List<Integer> Ids = qc.listCache();
			if (Ids != null)
				userPostList = QueryCache.idToObj(SUserPost.class, Ids);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return userPostList;
	}
	
}